package com.suneee.shop.util;

import java.util.ArrayList;
import java.util.List;

import com.suneee.core.util.StringUtils;
import com.suneee.orm.util.WhereClauseBuilder;

public class PrivateClauseBuilder extends WhereClauseBuilder 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5240474357331623450L;

	private StringBuilder sql;

	private List<Object> args;

	public PrivateClauseBuilder() {
		this("");
	}

	public PrivateClauseBuilder(String sql) {
		this.sql = new StringBuilder(sql);
		this.args = new ArrayList<Object>();
	}

	public void append(String str, Object... args){
	    this.sql.append(str);
		if(args != null && args.length > 0){
		    for (Object object : args) {
		        this.args.add(object);
            }
		}
	}
	
	/**
	 * 拼接等于条件
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder equals(String column, Object arg) {
		if (arg == null)
			return this;
		return add(column, "=?", arg);
	}

	public WhereClauseBuilder addCommonSQL(String column, Object arg){
		if (arg == null)
		return this;
		return add(column, "=?", arg);
	}

	public WhereClauseBuilder isNull(String column){
		and().append(column).append(" is null");
		return this;
	}

	public WhereClauseBuilder isNotNull(String column){
		and().append(column).append(" is not null");
		return this;
	}
	
	/**
	 * 拼接等于条件
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder equals(String column, String arg) {
		if (StringUtils.isEmpty(arg))
			return this;
		return add(column, "=?", arg);
	}
	
	/**
	 * 拼接等于条件
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder noEquals(String column, Object arg) {
		if (arg == null)
			return this;
		return add(column, "<>?", arg);
	}
	
	/**
	 * 拼接等于条件
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder noEquals(String column, String arg) {
		if (StringUtils.isEmpty(arg))
			return this;
		return add(column, "<>?", arg);
	}

	/**
	 * 拼接右模糊查询
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder startsWith(String column, String arg) {
		if (StringUtils.isEmpty(arg))
			return this;
		return add(column, " like ?", arg + "%");
	}

	/**
	 * 拼接模糊查询
	 * 
	 * @param column
	 * @param arg
	 */
	public WhereClauseBuilder contains(String column, String arg) {
		if (StringUtils.isEmpty(arg))
			return this;
		return add(column, " like ?", "%" + arg + "%");
	}

	/**
	 * 拼接小于条件
	 * 
	 * @param column
	 * @param arg
	 */
	public <T extends Comparable<T>> WhereClauseBuilder between(String column, T start, T end) {
		if (start == null && end == null) {
			return this;
		}
		if (start == null)
			return add(column, "<=?", end);
		if (end == null)
			return add(column, ">=?", start);
		args.add(start);
		return add(column, " between ? and ?", end);
	}

	/**
	 * 拼接in查询
	 * 
	 * @param column
	 * @param list
	 */
	public WhereClauseBuilder in(String column, List<Object> list) {
		if (list != null && !list.isEmpty()) {
			and().append(column).append(" in ");

			for (int i = 0, j = list.size(); i < j; ++i) {
				sql.append(i == 0 ? "(?" : ",?");
			}
			sql.append(')');
			args.addAll(list);
		}
		return this;
	}
	
	public WhereClauseBuilder orIn(String column, List<Object> list) {
		if (list != null && !list.isEmpty()) {
			or().append(column).append(" in ");

			for (int i = 0, j = list.size(); i < j; ++i) {
				sql.append(i == 0 ? "(?" : ",?");
			}
			sql.append(')');
			args.addAll(list);
		}
		return this;
	}
	
	public <T> WhereClauseBuilder inBaseType(String column, List<T> list) {
		if (list != null && !list.isEmpty()) {
			and().append(column).append(" in ");

			for (int i = 0, j = list.size(); i < j; ++i) {
				sql.append(i == 0 ? "(?" : ",?");
			}
			sql.append(')');
			args.addAll(list);
		}
		return this;
    }
	
	public WhereClauseBuilder appendWhere(String whereCommand) {
		if (whereCommand != null)		
			and().append(whereCommand);
		
		return this;
	}
	

	private StringBuilder and() {
		if (sql.length() > 0) {
			sql.append(" and ");
		}
		return sql;
	}
	
	private StringBuilder or() {
		if (sql.length() > 0) {
			sql.append(" or ");
		}
		return sql;
	}

	private WhereClauseBuilder add(String column, String op, Object arg) {
		and().append(column).append(op);
		args.add(arg);
		return this;
	}

	/**
	 * 获取创建的SQL
	 * 
	 * @return
	 */
	public String getSql() {
		String sqlStr = this.sql.toString();
		if (StringUtils.isEmpty(sqlStr)) {
			return " 1=1 ";
		}
		return sqlStr;
	}

	/**
	 * 获取创建SQL的参数
	 * 
	 * @return
	 */
	public Object[] getArgs() {
		return this.args.toArray();
	}
}
